
import java.io.*;
import java.net.*;
public class TCPServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			ServerSocket so=new ServerSocket(8888);
			while(true){
				String s1="";
				String s2="";
				Socket s=so.accept();
				System.out.println("server connect");
				BufferedReader br_j=new BufferedReader(new InputStreamReader(System.in));//键盘入
				BufferedReader br_socket=new BufferedReader(new InputStreamReader(s.getInputStream()));//socket收
				BufferedWriter bw=null;
				OutputStreamWriter osw=new OutputStreamWriter(s.getOutputStream());//socket发
				while((s1=br_socket.readLine())!=null){
					System.out.println("Client:"+s1);
					if(s1.equals("bb")){
						return;
					}
					if((s2=br_j.readLine())!=null){
						
						bw=new BufferedWriter(osw);//socket发
						bw.write(s2);
						bw.write("\r\n");
						bw.flush();
						System.out.println("Server:"+s2);
					}
				}
				bw.close();
				br_socket.close();
				br_j.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
